from sklearn.datasets import load_iris, fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier, export_graphviz


def knn_iris():
    """
    用KNN算法对鸢尾花进行分类
    :return:
    流程：
    1.获取数据
    2.数据集划分
    3.特征工程， 标准化
    4.KNN预估器流程
    5.模型评估
    """
    # 1）获取数据
    iris = load_iris()
    # 2）划分数据集
    x_train, x_test, y_train, y_test = \
        train_test_split(iris.data, iris.target, random_state=22)
    #random_state 随机数种子
    # 3）特征工程：标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    #这里不是用fit_transform,因为要统一标准
    #x_train和x_test需要使用同一个标准差和方差进行计算。fit是用于计算标准差和方差，因为要和train一致
    #因此test这里不用fit
    # 4）KNN算法预估器
    estimator = KNeighborsClassifier(n_neighbors=3)
    #n_neighbors 就是K的数值
    #注意：这里只使用到了fit，就是相当于训练，输入训练集的输入x和训练集的目标值y
    estimator.fit(x_train, y_train)
    # 5）模型评估
    # 方法1：直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("直接比对真实值和预测值:\n", y_test == y_predict)
    # 方法2：计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为：\n", score)
    return None

def knn_iris_gscv():
    """
    用KNN算法对鸢尾花进行分类，添加网格搜索和交叉验证
    :return:
    """
    # 1）获取数据
    iris = load_iris()
    # 2）划分数据集
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
    # 3）特征工程：标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 4）KNN算法预估器
    estimator = KNeighborsClassifier()
    # 加入网格搜索与交叉验证
    # 参数准备
    param_dict = {"n_neighbors": [1, 3, 5, 7, 9, 11]}
    estimator = GridSearchCV(estimator, param_grid=param_dict)
    # 验证集
    estimator.fit(x_train, y_train)
    # 5）模型评估
    # 方法1：直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("直接比对真实值和预测值:\n", y_test == y_predict)
    # 方法2：计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为：\n", score)
    # 最佳参数：best_params_
    print("最佳参数：\n", estimator.best_params_)
    # 最佳结果：best_score_
    print("最佳结果：\n", estimator.best_score_)
    # 最佳估计器：best_estimator_
    print("最佳估计器:\n", estimator.best_estimator_)
    # 交叉验证结果：cv_results_
    print("交叉验证结果:\n", estimator.cv_results_)
    return None

def nb_news():
    """
    用朴素贝叶斯算法对新闻进行分类
    :return:
    """
    # 1）获取数据
    news = fetch_20newsgroups(subset="all")
    # 2）划分数据集
    x_train, x_test, y_train, y_test = train_test_split(news.data, news.target)
    # 3）特征工程：文本特征抽取-tfidf
    transfer = TfidfVectorizer()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 4）朴素贝叶斯算法预估器流程
    estimator = MultinomialNB()
    estimator.fit(x_train, y_train)
    # 5）模型评估
    # 方法1：直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("直接比对真实值和预测值:\n", y_test == y_predict)
    # 方法2：计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为：\n", score)
    return None

def decision_iris():
    """
    用决策树对鸢尾花进行分类
    :return:
    """
    # 1）获取数据集
    iris = load_iris()
    # 2）划分数据集
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
    #决策树这里不用去标准化x_train这些
    # 3）决策树预估器
    estimator = DecisionTreeClassifier(criterion="entropy")
    estimator.fit(x_train, y_train)
    # 4）模型评估
    # 方法1：直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("直接比对真实值和预测值:\n", y_test == y_predict)
    # 方法2：计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为：\n", score)
    # 可视化决策树
    export_graphviz(estimator, out_file="data/iris_tree.dot", feature_names=iris.feature_names)
    return None

def suiji_titan():
    '''
    随机森林对泰坦尼克号生存人数预测
    :return:
    '''
    import pandas as pd
    # 1）获取数据
    titanic = pd.read_csv("data/titanic.csv")
    # print(titanic)
    # 筛选特征值和目标值
    x = titanic[['pclass', 'age', 'sex']]
    y = titanic['survived']
    # print(x.info())# 对当前选择的特征进行探查
    # print(x.head())
    # print(y.head())
    # 2）数据处理
    # ​		缺失值处理
    x['age'].fillna(x['age'].mean(), inplace=True)
    # print(x.info())
    # print(x['age'])
    # ​		类别转换成字典
    x = x.to_dict(orient="records")
    # print(x)
    # ​		准备好特征值，目标值
    # ​		数据划分
    from sklearn.model_selection import train_test_split, GridSearchCV
    x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)
    # 3）特征工程
    # ​		字典特征抽取
    from sklearn.feature_extraction import DictVectorizer

    transfer = DictVectorizer()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 4）随机森林预估器流程
    from sklearn.ensemble import RandomForestClassifier

    estimator = RandomForestClassifier()
    # 加入网格搜索与交叉验证
    # 参数准备
    param_dict = {"n_estimators": [100,200,300,500,800,900],"max_depth":[5,8,15,25,30]}
    estimator = GridSearchCV(estimator, param_grid=param_dict,cv=3)

    estimator.fit(x_train, y_train)
    # 5）模型评估
    # 方法一：直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("直接比对真实值和预测值：\n", y_test == y_predict)

    # 方法二:计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为:\n", score)

    # 最佳参数：best_params_
    print("最佳参数：\n", estimator.best_params_)
    # 最佳结果：best_score_
    print("最佳结果：\n", estimator.best_score_)
    # 最佳估计器：best_estimator_
    print("最佳估计器:\n", estimator.best_estimator_)
    # 交叉验证结果：cv_results_
    print("交叉验证结果:\n", estimator.cv_results_)

    # 可视化决策树
    # export_graphviz(estimator, out_file="data/titanic_tree.dot", feature_names=transfer.get_feature_names())


if __name__ == '__main__':
    #代码1：用KNN算法对鸢尾花进行分类
    # knn_iris()
    #代码2：用KNN算法对鸢尾花进行分类，添加网格搜索和交叉验证
    # knn_iris_gscv()
    #代码3:用朴素贝叶斯算法对新闻进行分类
    # nb_news()
    # 代码4：用决策树对鸢尾花进行分类
    # decision_iris()
    #代码5:随机森林对泰坦尼克号生存人数预测
    suiji_titan()